package com.amap.api.col.s;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import com.amap.api.col.s.cf;
import com.amap.api.col.s.y;
import com.amap.api.services.cloud.CloudItemDetail;
import com.amap.api.services.cloud.CloudResult;
import com.amap.api.services.cloud.CloudSearch;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.interfaces.ICloudSearch;
import java.util.HashMap;
import java.util.List;

/* compiled from: CloudSearchCore.java */
/* loaded from: classes.dex */
public final class bi implements ICloudSearch {
    private Context a;
    private CloudSearch.OnCloudSearchListener b;
    private CloudSearch.Query c;
    private int d;
    private HashMap<Integer, CloudResult> e;
    private Handler f;

    public bi(Context context) throws AMapException {
        cg a = cf.a(context, m.a(false));
        if (a.a != cf.c.SuccessCode) {
            throw new AMapException(a.b, 1, a.b, a.a.a());
        }
        this.a = context.getApplicationContext();
        this.f = y.a();
    }

    @Override // com.amap.api.services.interfaces.ICloudSearch
    public final void setOnCloudSearchListener(CloudSearch.OnCloudSearchListener onCloudSearchListener) {
        this.b = onCloudSearchListener;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r1v13, types: [com.amap.api.services.cloud.CloudResult] */
    /* JADX WARN: Type inference failed for: r1v20 */
    /* JADX WARN: Type inference failed for: r1v21 */
    /* JADX WARN: Type inference failed for: r1v7, types: [int] */
    /* JADX WARN: Type inference failed for: r1v8 */
    public CloudResult a(CloudSearch.Query query) throws AMapException {
        CloudResult cloudResult = null;
        try {
        } catch (Throwable th) {
            th = th;
        }
        if (!b(query)) {
            throw new AMapException(AMapException.AMAP_CLIENT_INVALID_PARAMETER);
        }
        if (!query.queryEquals(this.c)) {
            this.d = 0;
            this.c = query.m14clone();
            HashMap<Integer, CloudResult> hashMap = this.e;
            if (hashMap != null) {
                hashMap.clear();
            }
        }
        ?? r1 = this.d;
        try {
        } catch (Throwable th2) {
            th = th2;
            cloudResult = r1;
            n.a(th, "CloudSearch", "searchCloud");
            if (th instanceof AMapException) {
                throw th;
            }
            th.printStackTrace();
            return cloudResult;
        }
        if (r1 == 0) {
            CloudResult c = new l(this.a, query).c();
            a(c, query);
            r1 = c;
        } else {
            cloudResult = a(query.getPageNum());
            if (cloudResult == null) {
                CloudResult c2 = new l(this.a, query).c();
                this.e.put(Integer.valueOf(query.getPageNum()), c2);
                r1 = c2;
            }
            return cloudResult;
        }
        return r1;
    }

    @Override // com.amap.api.services.interfaces.ICloudSearch
    public final void searchCloudAsyn(final CloudSearch.Query query) {
        try {
            ax.a().a(new Runnable() { // from class: com.amap.api.col.s.bi.1
                {
                    bi.this = this;
                }

                @Override // java.lang.Runnable
                public final void run() {
                    Message obtainMessage = y.a().obtainMessage();
                    try {
                        try {
                            obtainMessage.arg1 = 12;
                            obtainMessage.what = 700;
                            y.e eVar = new y.e();
                            eVar.b = bi.this.b;
                            obtainMessage.obj = eVar;
                            eVar.a = bi.this.a(query);
                            obtainMessage.arg2 = 1000;
                        } catch (AMapException e) {
                            obtainMessage.arg2 = e.getErrorCode();
                        }
                    } finally {
                        bi.this.f.sendMessage(obtainMessage);
                    }
                }
            });
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public CloudItemDetail a(String str, String str2) throws AMapException {
        if (str == null || str.trim().equals("")) {
            throw new AMapException(AMapException.AMAP_CLIENT_INVALID_PARAMETER);
        }
        if (str2 == null || str2.trim().equals("")) {
            throw new AMapException(AMapException.AMAP_CLIENT_INVALID_PARAMETER);
        }
        try {
            return new k(this.a, new ai(str, str2)).c();
        } catch (Throwable th) {
            n.a(th, "CloudSearch", "searchCloudDetail");
            if (th instanceof AMapException) {
                throw th;
            }
            th.printStackTrace();
            return null;
        }
    }

    @Override // com.amap.api.services.interfaces.ICloudSearch
    public final void searchCloudDetailAsyn(final String str, final String str2) {
        try {
            ax.a().a(new Runnable() { // from class: com.amap.api.col.s.bi.2
                {
                    bi.this = this;
                }

                @Override // java.lang.Runnable
                public final void run() {
                    Message obtainMessage = y.a().obtainMessage();
                    try {
                        try {
                            obtainMessage.arg1 = 12;
                            obtainMessage.what = 701;
                            y.d dVar = new y.d();
                            dVar.b = bi.this.b;
                            obtainMessage.obj = dVar;
                            dVar.a = bi.this.a(str, str2);
                            obtainMessage.arg2 = 1000;
                        } catch (AMapException e) {
                            obtainMessage.arg2 = e.getErrorCode();
                        }
                    } finally {
                        bi.this.f.sendMessage(obtainMessage);
                    }
                }
            });
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    private void a(CloudResult cloudResult, CloudSearch.Query query) {
        HashMap<Integer, CloudResult> hashMap = new HashMap<>();
        this.e = hashMap;
        if (this.d > 0) {
            hashMap.put(Integer.valueOf(query.getPageNum()), cloudResult);
        }
    }

    private CloudResult a(int i) {
        if (!b(i)) {
            throw new IllegalArgumentException("page out of range");
        }
        return this.e.get(Integer.valueOf(i));
    }

    private boolean b(int i) {
        return i <= this.d && i > 0;
    }

    private static boolean b(CloudSearch.Query query) {
        if (query == null || n.a(query.getTableID()) || query.getBound() == null) {
            return false;
        }
        if (query.getBound() != null && query.getBound().getShape().equals("Bound") && query.getBound().getCenter() == null) {
            return false;
        }
        if (query.getBound() != null && query.getBound().getShape().equals("Rectangle")) {
            LatLonPoint lowerLeft = query.getBound().getLowerLeft();
            LatLonPoint upperRight = query.getBound().getUpperRight();
            if (lowerLeft == null || upperRight == null || lowerLeft.getLatitude() >= upperRight.getLatitude() || lowerLeft.getLongitude() >= upperRight.getLongitude()) {
                return false;
            }
        }
        if (query.getBound() == null || !query.getBound().getShape().equals("Polygon")) {
            return true;
        }
        List<LatLonPoint> polyGonList = query.getBound().getPolyGonList();
        for (int i = 0; i < polyGonList.size(); i++) {
            if (polyGonList.get(i) == null) {
                return false;
            }
        }
        return true;
    }
}
